package com.yc.demo.controller;

import com.alibaba.fastjson.JSON;
import com.yc.demo.entity.DemoEntity;
import com.yc.demo.service.IDemoService;
import com.yc.demo.vo.MessageDTO;
import com.yc.demo.vo.ResultBody;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>控制器入口</p>
 *
 * @author: YuanChilde
 * @date: 2019-12-19 14:11
 * @version: 1.0
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2019-12-19 14:11    YuanChilde     1.0        新增
 */
@RestController
@RequestMapping(value = "/demo", produces = {"application/json"})
public class DemoController {

    @Autowired
    private IDemoService demoService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * post请求 json数据
     * @param entity
     * @return
     */
    @PostMapping(value = "/save")
    public ResultBody save(@RequestBody DemoEntity entity) {
        demoService.save(entity);
        return ResultBody.ok();
    }

    @GetMapping("/get")
    public ResultBody get(@RequestParam("idx") Long id) {
        DemoEntity demoEntity = demoService.getById(id);
        return ResultBody.ok().put("data",demoEntity);
    }

    private ResultBody sub(Long id) {
        return ResultBody.ok();
    }

    @GetMapping("/sendMg")
    public ResultBody sendMg() {
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setContent("test");
/*        MessageBuilder messageBuilder = MessageBuilder.withPayload(messageDTO);
        messageBuilder.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        messageBuilder.setHeader("type", "sign");*/
        rocketMQTemplate.send("xx-topic:sign", MessageBuilder.withPayload(JSON.toJSON(messageDTO)).build());
        return ResultBody.ok();
    }
    @GetMapping("/sendMg1")
    public ResultBody sendMg1() {
        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setContent("test");
/*        MessageBuilder messageBuilder = MessageBuilder.withPayload(messageDTO);*/
/*        messageBuilder.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
        messageBuilder.setHeader("type", "sign2");*/
        rocketMQTemplate.send("xx-topic:sign2", MessageBuilder.withPayload(JSON.toJSON(messageDTO)).build());
        return ResultBody.ok();
    }
}
